package pit.com.ordermobilesystem.adapter;



import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.http.OrderSystemHttpService;
import pit.com.ordermobilesystem.imageutil.ImageFetcher;
import pit.com.ordermobilesystem.model.Category;
import pit.com.ordermobilesystem.model.Item;
import pit.com.ordermobilesystem.modeladapter.ChildView;
import pit.com.ordermobilesystem.modeladapter.ParentView;
import pit.com.ordermobilesystem.provider.OrderMobileProvider;
import pit.com.ordermobilesystem.sharepreference.OrderSharePreference;
import pit.com.ordermobilesystem.util.OrderConstant;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorTreeAdapter;

public class OrderMobileAdapter extends CursorTreeAdapter {
	private ImageFetcher mImageWorker;
	public int nGroup;
	Context mcontext;
	Cursor mcursor;

	private String post = OrderConstant.PORT;
	private String sever_host = OrderConstant.SERVER_HOST;
	
	public OrderMobileAdapter(Cursor cursor, Context context) {
		super(cursor, context);
		// TODO Auto-generated constructor stub
		
		this.mcursor = cursor;
		this.mcontext = context;
		nGroup = cursor.getColumnCount();
		mImageWorker = new ImageFetcher(context, 100, 100);
		mImageWorker.setLoadingImage(R.drawable.chickencurry);
		if (!OrderSharePreference.getDownloadSync(context)) {
			mImageWorker.setOfflineMode(true);
		}
		
	}

	@Override
	protected Cursor getChildrenCursor(Cursor groupCursor) {
		// TODO Auto-generated method stub
		Category category = new Category();
		OrderMobileProvider.getCategory(groupCursor, category);
		Cursor data =mcontext.getContentResolver().query(OrderMobileProvider.ITEM_CONTENT_URI,null,
				OrderMobileProvider.ItemTable.ID_CATEGORY+"="+category.getId(), null, null);		
		return data;
	}

	@Override
	protected void bindChildView(View view, Context context, Cursor cursor,	boolean isLastChild) {
		// TODO Auto-generated method stub
		Item item = new Item();
		OrderMobileProvider.getItem(cursor, item);
		ChildView child = (ChildView) view;		
		String url= OrderSystemHttpService.getImageProductURL(sever_host, post,item.getLinkImage()+".jpg");	
		mImageWorker.loadImage(url,child.img_dish);
		child.tv_nameitem.setText(item.getNameItem());
		int tien = (int) item.getPrice();
		child.tv_price.setText("Giá: "+String.valueOf(tien)+".000 đ");
		child.tv_discount.setText("Giảm: "+String.valueOf(item.getDiscount())+" Đ");
//		child.tv_description.setText(item.getDescription());		
		child.setTag(item);

	}

	@Override
	protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
		// TODO Auto-generated method stub
		Category category = new Category();
		OrderMobileProvider.getCategory(cursor, category);
		String NameCategory = category.getNameCategory();
		ParentView group = (ParentView) view;
		
		group.tv_category.setText(NameCategory);
		
	}

	@Override
	protected View newChildView(Context context, Cursor cursor,
			boolean isLastChild, ViewGroup parent) {
		// TODO Auto-generated method stub
		ChildView child = new ChildView(context);
		return child;
	}

	@Override
	protected View newGroupView(Context context, Cursor cursor,
			boolean isExpanded, ViewGroup parent) {
		// TODO Auto-generated method stub
		ParentView group = new ParentView(context);
		return group;
	}

}